package com.ldx.eduline.common.utils;

import com.ldx.eduline.common.bean.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class List2Tree {
    /**
     * 方式一：使用两层循环实现
     * @return
     */
    public static List<TreeNode> toTree(List<TreeNode> treeList) {
        List<TreeNode> retList = new ArrayList<>();

        for (TreeNode parent : treeList) {
            if ("0".equals(parent.getParentId())) {
                retList.add(parent);
            }

            for (TreeNode child : treeList) {
                if (child.getParentId() == parent.getId()) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(child);
                }
            }
        }
        return retList;
    }

}
